package com.example.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.study.common.api.ResultUtil;
import com.example.study.mapper.ClassMapper;
import com.example.study.mapper.StudentMapper;
import com.example.study.mapper.TeacherMapper;
import com.example.study.model.*;
import com.example.study.service.ClassService;
import com.fasterxml.jackson.databind.JsonSerializable;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ClassServiceImpl extends ServiceImpl<ClassMapper, ClassPO> implements ClassService {

    @Resource
    private ClassMapper classMapper;

    @Resource
    private StudentMapper studentMapper;

    @Resource
    private TeacherMapper teacherMapper;

    @Override
    public ResultUtil saveClass(ClassVO clazz) {
        log.info("saveClass params: {}", clazz);
        ClassPO classPO = new ClassPO();
        BeanUtils.copyProperties(clazz, classPO);
        log.info("insert data params: {}", classPO.toString());
        int row = classMapper.insert(classPO);
        if (row > 0){
            log.info("saveClass success end. {}", clazz);
            return ResultUtil.success(clazz);
        }

        log.info("saveClass fail end. {}", clazz);
        return ResultUtil.failed("创建班级失败");
    }

    @Override
    public ResultUtil getClassList() {
        List<Object> list = classMapper.selectObjs(null);
        return ResultUtil.success(list);
    }

    @Override
    public ResultUtil getClassPage(int pageIndex, int pageSize) {
        Page<ClassPO> page = new Page<>(pageIndex, pageSize);
        IPage<ClassPO> iPage = classMapper.selectPage(page, null);
        return ResultUtil.success(iPage);
    }

    @Override
    public ResultUtil getClassById(Long id) {
        ClassPO classPO = classMapper.selectById(id);
        return ResultUtil.success(classPO);
    }

    @Override
    public ResultUtil getStuTeachByClassId(Long classId) {
        QueryWrapper<StudentPO> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.select("student_code", "student_name", "student_sex", "student_age")
                .eq("class_id", classId);
        List<StudentPO> students = studentMapper.selectList(studentQueryWrapper);

        QueryWrapper<TeacherPO> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq(true, "class_id", classId);
        List<TeacherPO> teachers = teacherMapper.selectList(teacherQueryWrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("classId", classId);
        map.put("student", students);
        map.put("teacher", teachers);
        return ResultUtil.success(map);
    }
}
